package com.cb.controller.admin.order;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import cn.cloud.utils.DateUtil;
import cn.cloud.utils.StringUtils;

import com.cb.core.command.QueryFilter;
import com.cb.core.command.QueryResult;
import com.cb.core.controller.BaseListController;
import com.cb.core.webapp.paging.PagingBean;
import com.cb.domain.model.order.BOrder;
import com.cb.domain.model.order.Flight;
import com.cb.domain.model.order.OrderTotal;
import com.cb.domain.service.order.BOrderManager;

/**
 * 
 * @Author author
 * @Vesion 1.0
 * Create Time:
 * <p><b>Class Description:</b>[TODO]<p>
 */
public class OrderReportController extends BaseListController {
	
	@Autowired
	private BOrderManager bOrderManager;
	
	/**对象列表页，提供最基本的分页显示
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//String createTime=request.getParameter("createTime2");
		QueryResult bOrders=bOrderManager.getAll(new QueryFilter(request));
		
		return new ModelAndView("admin/order/nationalReport","bOrders",bOrders);
	}
	
	
	/**已登记退票报表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView applyReturnTicketReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		QueryFilter filter=new QueryFilter(request);
		filter.addFilter("Q_status_L_EQ", "8");
		QueryResult bOrders=bOrderManager.getAll(filter);
		
		return new ModelAndView("admin/order/applyReturnTicketReport","bOrders",bOrders);
	}
	
	/**已申请的退票报表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView applyRefundReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		QueryFilter filter=new QueryFilter(request);
		filter.addFilter("Q_status_L_EQ", "31");
		QueryResult bOrders=bOrderManager.getAll(filter);
		
		return new ModelAndView("admin/order/applyRefundReport","bOrders",bOrders);
	}
	
	/**退款完成订单报表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView refundOKReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		QueryFilter filter=new QueryFilter(request);
		filter.addFilter("Q_status_L_EQ", "39");
		QueryResult bOrders=bOrderManager.getAll(filter);
		
		return new ModelAndView("admin/order/refundOKReport","bOrders",bOrders);
	}
	
	/**
	 * 特殊产品报表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * @author qiaoqd 
	 */
	public ModelAndView tscpReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QueryResult bOrders=bOrderManager.getAll(new QueryFilter(request));
		
		return new ModelAndView("admin/order/tscpReport","bOrders",bOrders);
	}
	/**
	 * 特殊产品报表明细
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * @author qiaoqd 
	 */
	public ModelAndView tscpReportDetail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String orderId=request.getParameter("id");
		BOrder order=null;
		if(orderId!=null&&!orderId.equals("")){
			order=bOrderManager.get(Long.parseLong(orderId));
		}
		
		return new ModelAndView("admin/order/tscp_chakan","order",order);
	}
	
	/**保险对账明细
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView insuranceReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QueryResult bOrders=bOrderManager.getAll(new QueryFilter(request));
		
		return new ModelAndView("admin/order/insuranceReport","bOrders",bOrders);
	}
	
	/**销售汇总
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView saleTotal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		

		String beginTime=request.getParameter("beginTime");
		String endTime=request.getParameter("endTime");
		 
		PagingBean pageBean=new PagingBean();
		pageBean.setPageSize(50);
		QueryResult bOrders=new QueryResult(pageBean);
		List <BOrder> list=bOrderManager.getbOrderByTime(beginTime,endTime,pageBean);
		List <OrderTotal> oderTotals=new ArrayList<OrderTotal>();
//		销售列表
		for(BOrder b:list){
			OrderTotal order=new OrderTotal();
			order.setCreateTime(b.getCreateTime());
			order.setOrderNo(b.getOrderNo());
			if(b.getStatus()==2){
				order.setPayCount(b.getPassengers().size());//成功支付量
				order.setFaultCount(0);//退票量
				order.setAllPrice(setDoubleValue(b.getViewPrice().doubleValue()).longValue());//销售净额（不含税金）  
				order.setConstructionFee(setDoubleValue(b.getConstructionFee()));//	机场建设费金额  
				order.setFuelTax(setDoubleValue(b.getFuelTax()));//	燃油税金额  
				order.setReturnPrice((double)0);//	退款净额（即不含税金的票面额） 
				order.setReturnConstructionFee((double)0);//	退机场建设费金额 
				order.setRetrunFuelTax((double)0);//	退燃油税金额   
				order.setReturnAllPrice((double)0);//退款金额 
				order.setReturnTicketPrice((double)0);//	退票费
				order.setXcdPrice(setDoubleValue(b.getXcdPrice().doubleValue()).longValue());//	快递费 
				order.setTotalPrice(setDoubleValue(b.getPrice().doubleValue()).longValue());//	汇总(收入)金额
			}else{
				order.setPayCount(0);//成功支付量
				order.setFaultCount(b.getPassengers().size());//退票量
				order.setAllPrice((long)0);//销售净额（不含税金）  
				order.setConstructionFee((double)0);//	机场建设费金额  
				order.setFuelTax((double)0);//	燃油税金额  
				order.setReturnPrice(setDoubleValue(b.getViewPrice().doubleValue()));//	退款净额（即不含税金的票面额）
				order.setReturnConstructionFee(setDoubleValue(b.getConstructionFee()));//	退机场建设费金额 
				order.setRetrunFuelTax(setDoubleValue(b.getFuelTax()));//	退燃油税金额 
				order.setReturnAllPrice(setDoubleValue(b.getPrice().doubleValue())-setDoubleValue(b.getRefundFee()));//退款金额 
				order.setReturnTicketPrice(setDoubleValue(b.getRefundFee()));//	退票费
				order.setXcdPrice((long)0);//	快递费 
				order.setTotalPrice(-(setDoubleValue(b.getPrice().doubleValue()).longValue()-setDoubleValue(b.getRefundFee()).longValue()));//	汇总(收入)金额
			}
			String cabin="";//	舱位 
			for(Flight f:b.getFlights()){
				cabin+=f.getCabin()+",";
			}
			if(cabin!=""){
				cabin=cabin.substring(0,cabin.length()-1);
			}
			order.setCabin(cabin);
			oderTotals.add(order);
		}
		
//		销售汇总
		if(!StringUtils.isEmpty(beginTime)){
		List <String> dates=getDateList(beginTime, endTime);
		List <OrderTotal> oderTotalbak=new ArrayList<OrderTotal>();
			for(String date:dates){
				for(BOrder b:list){
					if(date.equals(DateUtil.getInstance().formatDate2(b.getCreateTime()))){
						boolean ishere=true;
						for(OrderTotal bak:oderTotalbak){
							if(DateUtil.getInstance().formatDate2(bak.getCreateTime()).equals(date)){
//								bak.setCreateTime(b.getCreateTime());
//								bak.setOrderNo(b.getOrderNo());
								if(b.getStatus()==2){
									bak.setPayCount(bak.getPayCount()+b.getPassengers().size());//成功支付量
									bak.setFaultCount(bak.getFaultCount());//退票量
									bak.setAllPrice(bak.getAllPrice()+setDoubleValue(b.getViewPrice().doubleValue()).longValue());
									bak.setConstructionFee(bak.getConstructionFee()+setDoubleValue(b.getConstructionFee()));
									bak.setFuelTax(bak.getFuelTax()+setDoubleValue(b.getFuelTax()));
									bak.setReturnPrice(bak.getReturnPrice());//退款净额
									bak.setReturnConstructionFee(bak.getReturnConstructionFee());
									bak.setRetrunFuelTax(bak.getRetrunFuelTax());
									bak.setReturnAllPrice(bak.getReturnAllPrice());//退款金额 
									bak.setReturnTicketPrice(bak.getReturnTicketPrice());//	退票费
									bak.setXcdPrice(bak.getXcdPrice()+setDoubleValue(b.getXcdPrice().doubleValue()).longValue());
									bak.setTotalPrice(bak.getTotalPrice()+setDoubleValue(b.getPrice().doubleValue()).longValue());
								}else{
									bak.setPayCount(bak.getPayCount());//成功支付量
									bak.setFaultCount(bak.getFaultCount()+b.getPassengers().size());//退票量
									bak.setAllPrice(bak.getAllPrice());
									bak.setConstructionFee(bak.getConstructionFee());
									bak.setFuelTax(bak.getFuelTax());
									bak.setReturnPrice(bak.getReturnPrice()+setDoubleValue(b.getViewPrice().doubleValue()));//退款净额
									bak.setReturnConstructionFee(bak.getReturnConstructionFee()+setDoubleValue(b.getConstructionFee()));
									bak.setRetrunFuelTax(bak.getRetrunFuelTax()+setDoubleValue(b.getFuelTax()));
									bak.setReturnAllPrice(bak.getReturnAllPrice()+setDoubleValue(b.getPrice().doubleValue())-setDoubleValue(b.getRefundFee()));//退款金额 
									bak.setReturnTicketPrice(bak.getReturnTicketPrice()+setDoubleValue(b.getRefundFee()));//	退票费
									bak.setXcdPrice(bak.getXcdPrice());	
									bak.setTotalPrice(bak.getTotalPrice()-(setDoubleValue(b.getPrice().doubleValue()).longValue()-setDoubleValue(b.getRefundFee()).longValue()));
								}
								ishere=false;
							}
						}
						if(ishere){
							OrderTotal order=new OrderTotal();
							order.setCreateTime(DateUtil.getInstance().parseDate2(date));
						if(b.getStatus()==2){
							order.setPayCount(b.getPassengers().size());//成功支付量
							order.setFaultCount(0);//退票量
							order.setAllPrice(setDoubleValue(b.getViewPrice().doubleValue()).longValue());//销售净额（不含税金）  
							order.setConstructionFee(setDoubleValue(b.getConstructionFee()));//	机场建设费金额  
							order.setFuelTax(setDoubleValue(b.getFuelTax()));//	燃油税金额  
							order.setReturnPrice((double)0);//	退款净额（即不含税金的票面额） 
							order.setReturnConstructionFee((double)0);//	退机场建设费金额 
							order.setRetrunFuelTax((double)0);//	退燃油税金额   
							order.setReturnAllPrice((double)0);//退款金额 
							order.setReturnTicketPrice((double)0);//	退票费
							order.setXcdPrice(setDoubleValue(b.getXcdPrice().doubleValue()).longValue());//	快递费 
							order.setTotalPrice(setDoubleValue(b.getPrice().doubleValue()).longValue());//	汇总(收入)金额
						}else{
							order.setPayCount(0);//成功支付量
							order.setFaultCount(b.getPassengers().size());//退票量
							order.setAllPrice((long)0);//销售净额（不含税金）  
							order.setConstructionFee((double)0);//	机场建设费金额  
							order.setFuelTax((double)0);//	燃油税金额  
							order.setReturnPrice(setDoubleValue(b.getViewPrice().doubleValue()));//	退款净额（即不含税金的票面额）
							order.setReturnConstructionFee(setDoubleValue(b.getConstructionFee()));//	退机场建设费金额 
							order.setRetrunFuelTax(setDoubleValue(b.getFuelTax()));//	退燃油税金额 
							order.setReturnAllPrice(setDoubleValue(b.getPrice().doubleValue())-setDoubleValue(b.getRefundFee()));//退款金额 
							order.setReturnTicketPrice(setDoubleValue(b.getRefundFee()));//	退票费
							order.setXcdPrice((long)0);//	快递费 
							order.setTotalPrice(-(setDoubleValue(b.getPrice().doubleValue()).longValue()-setDoubleValue(b.getRefundFee()).longValue()));//	汇总(收入)金额
						}
							oderTotalbak.add(order);
						}
					}
				}
			}
			for(OrderTotal bak:oderTotalbak){
				oderTotals.add(bak);
			}
		}
		
		bOrders.setList(oderTotals);
		bOrders.setFullListSize(oderTotals.size());
		return new ModelAndView("admin/order/saleTotalReport","bOrders",bOrders);
	}
	
	public List<String> getDateList(String stratDate ,String endDate){
		List<String> date=new ArrayList();
		date.add(stratDate);
		Calendar now = Calendar.getInstance();   
		    int add=1;
		    while (!stratDate.equals(endDate)) {
		    	try {
					now.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(stratDate));
				} catch (ParseException e) {
					e.printStackTrace();
				}
		    	now.add(Calendar.DATE, add);
		    	stratDate=new SimpleDateFormat("yyyy-MM-dd").format(now.getTime());
		    	date.add(stratDate);
			}
		    return date;
	}
	
	public Double setDoubleValue(Double dou){
		if(dou==null){
			return (double)0;
		}
		return dou;
	}
	
}
